
#include "chapter1_2.h"

using namespace std;


class Base
{
public:
	string name;
	virtual void set( ) = 0;
};

class DerivedA:public Base
{
public:
	void set( ) { _DebugPrint("\n%s",__FUNCTION__);};

};

class DerivedB:public Base
{
public:
	void set( ) { _DebugPrint("\n%s",__FUNCTION__);};

};

void test_rtti()
{
	Base *pb;

	DerivedA drvA, *pdrva;
	DerivedB drvB, *pdrvb;

	pb = &drvA;
	pb->set();
	pb = &drvB;
	pb->set();

	pdrva = dynamic_cast<DerivedA*>(pb);
	if(pdrva)
	{
		_DebugPrint("\nCurrent run type is DerivedA !");
	}
	else
	{
		_DebugPrint("\nCurrent run type is DerivedB !");
	}

	DerivedB &rb = dynamic_cast<DerivedB &>(*pb);
	//DerivedA &ra = dynamic_cast<DerivedA &>(*pb);
	// try catch
	try{DerivedA &ra = dynamic_cast<DerivedA &>(*pb);}
	catch(std::bad_cast){
		_DebugPrint("\n Current run type is DerivedB !");
		_DebugPrint("\n%s", typeid(rb.name).name());
		_DebugPrint("\n%s", typeid(pb).name());
	
	}

}